{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "主管无权批假\n",
      "经理批6天假\n"
     ]
    }
   ],
   "source": [
    "from abc  import abstractmethod,ABCMeta\n",
    "class handler(metaclass=ABCMeta):\n",
    "    @abstractmethod\n",
    "    def handler_leave(self,day):\n",
    "        pass\n",
    "class gmh(handler):\n",
    "    def handler_leave(self,day):\n",
    "        if int(day) < 10:\n",
    "            print(\"经理批%d天假\"%day)\n",
    "        else:\n",
    "            print('呵呵')\n",
    "class dmh(handler):\n",
    "    def __init__(self):\n",
    "        self.successor=gmh()\n",
    "    def handler_leave(self,day):\n",
    "        if int(day)<7:\n",
    "            print('经理批{}天假'.format(day))\n",
    "        else:\n",
    "            print('经理无权批假')\n",
    "            self.succesor.handler_leave(day)\n",
    "class pdh(handler):\n",
    "    def __init__(self):\n",
    "        self.successor=dmh()\n",
    "    def handler_leave(self,day):\n",
    "        if int(day)<3:\n",
    "            print('主管批%d天假'%day)\n",
    "        else:\n",
    "            print('主管无权批假')\n",
    "            self.successor.handler_leave(day)\n",
    "day=input()\n",
    "h=pdh()\n",
    "h.handler_leave(day)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'event_handler'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-20-a721e54be210>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     33\u001b[0m \u001b[0mdiv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'children'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     34\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'event_handler'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mbodyhandler\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 35\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdiv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'event_handler'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m456\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     36\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     37\u001b[0m \u001b[0mdiv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'event_handler'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0melementhandler\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdiv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'father'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'event_handler'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'event_handler'"
     ]
    }
   ],
   "source": [
    "from abc import ABCMeta,abstractmethod\n",
    "class handler(metaclass=ABCMeta):\n",
    "    @abstractmethod\n",
    "    def add_leave(self,func):\n",
    "        pass \n",
    "    @abstractmethod\n",
    "    def handler(self):\n",
    "        pass\n",
    "class bodyhandler(handler):\n",
    "    def __init__(self):\n",
    "        self.func=None\n",
    "    def add_leave(self,func):\n",
    "        self.func=func\n",
    "    def handler(self):\n",
    "        if self.func:\n",
    "            return self.func()\n",
    "        else:\n",
    "            print('最后一级无法处理')\n",
    "class elementhandler(handler):\n",
    "    def __init__(self,successor):\n",
    "        self.successor=successor\n",
    "    def add_leave(self,func):\n",
    "        self.func=func\n",
    "    def handler(self):\n",
    "        if self.func:\n",
    "            return self.func\n",
    "        else:\n",
    "            return self.successor.handler()\n",
    "body={'type':'body','name':'body','children':[],'father':None}\n",
    "div={'type':'div','name':'div','children':[],'father':body}\n",
    "a={'type':'a','name':'a','children':[],'father':div}\n",
    "body['children'].append(div)\n",
    "div['children'].append(a)\n",
    "body['event_handler']=bodyhandler()\n",
    "\n",
    "\n",
    "div['event_handler']=elementhandler(div['father']['event_handler'])\n",
    "a['event_handler']=elementhandler(a['father']['event_handler'])\n",
    "def attach_event(element,func):\n",
    "    element['event_handler'].add_leave(func)\n",
    "def func_div():\n",
    "    print('sdfsaf')\n",
    "def func_a():\n",
    "    print('a')\n",
    "def func_body():\n",
    "    print('body')\n",
    "attach_event(a,func_a)\n",
    "a['event_handler'].handler()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fujun\n"
     ]
    }
   ],
   "source": [
    "d={'name':'fujun','age':45,'fwen':'open'}\n",
    "res=d['name']\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
